/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jetbrains.commandInterface.commandLine.psi.impl;

import com.intellij.psi.PsiElement;
import com.jetbrains.commandInterface.command.Argument;
import com.jetbrains.commandInterface.command.Help;
import com.jetbrains.commandInterface.command.Option;
import com.jetbrains.commandInterface.commandLine.CommandLinePart;
import com.jetbrains.commandInterface.commandLine.ValidationResult;
import com.jetbrains.commandInterface.commandLine.psi.CommandLineArgument;
import com.jetbrains.commandInterface.commandLine.psi.CommandLineFile;
import com.jetbrains.commandInterface.commandLine.psi.CommandLineOption;
import com.jetbrains.python.psi.PyUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Class to be used by autogenerated PSI elements to delegate logic
 *
 * @author Ilya.Kazakevich
 */
@SuppressWarnings("StaticMethodOnlyUsedInOneClass") // This class was created to store functions for grammar kit
final class CommandLinePsiImplUtils {


  private CommandLinePsiImplUtils() {
  }

  /**
   * Checks if option is long (--long-option vs -s)
   *
   * @param o option
   * @return true if long
   */
  static boolean isLong(@NotNull final CommandLineOption o) {
    return o.getLongOptionNameToken() != null;
  }

  /**
   * For options with eq finds argument next to it.
   * For options with out of eq just use next psi
   * @return null if option does not have eq
   */
  @Nullable
  static CommandLineArgument findArgument(@NotNull final CommandLineOption option) {
    if (option.getText().endsWith("=")) {
      return PyUtil.as(option.getNextSibling(), CommandLineArgument.class);
    }
    return null;
  }
  /**
   * Finds real option based on psi opton
   *
   * @param option psi option
   * @return real option (if any)
   */
  @Nullable
  static Option findRealOption(@NotNull final CommandLineOption option) {
    final ValidationResult validationResult = getValidationResult(option);
    if (validationResult == null) {
      return null;
    }
    return validationResult.getOption(option);
  }

  /**
   * @return for arg in quotes returns bare value, or simply value otherwise
   */
  @NotNull
  static String getValueNoQuotes(@NotNull final CommandLineArgument argument) {
    final char[] chars = argument.getText().toCharArray();
    if (chars.length == 0) {
      return "";
    }
    final char firstChar = chars[0];
    if (firstChar == chars[chars.length - 1] && firstChar == '"' || firstChar == '\'') {
      return argument.getText().substring(1, argument.getTextLength() - 1);
    }
    return argument.getText();
  }
  /**
   * Tries to find appropriate help for argument. It can be argument help for positional argument or option help
   * for option argument.
   *
   * @param argument argument to search help for
   * @return help for argument or null if not found
   */
  @Nullable
  static Help findBestHelp(@NotNull final CommandLineArgument argument) {
    final Option option = argument.findOptionForOptionArgument();
    if (option != null) {
      return option.getHelp();
    }
    final Argument realArgument = argument.findRealArgument();
    return (realArgument != null ? realArgument.getHelp() : null);
  }


  /**
   * Finds real argument based on psi argument
   *
   * @param argument psi argument
   * @return real argument (if any)
   */
  @Nullable
  static Argument findRealArgument(@NotNull final CommandLineArgument argument) {
    final ValidationResult validationResult = getValidationResult(argument);
    if (validationResult == null) {
      return null;
    }
    return validationResult.getArgument(argument);
  }

  /**
   * Finds option if argument is option argument
   *
   * @param argument argument to check
   * @return option (if option argument) or null if not
   */
  @Nullable
  static Option findOptionForOptionArgument(@NotNull final CommandLineArgument argument) {
    final ValidationResult validationResult = getValidationResult(argument);
    if (validationResult == null) {
      return null;
    }
    return validationResult.getOptionForOptionArgument(argument);
  }

  /**
   * Searches for validation result for command line
   *
   * @param commandLinePart command line part
   * @return validation result (if any)
   */
  @Nullable
  private static ValidationResult getValidationResult(@NotNull final CommandLinePart commandLinePart) {
    final CommandLineFile commandLineFile = commandLinePart.getCommandLineFile();
    if (commandLineFile == null) {
      return null;
    }
    final ValidationResult validationResult = commandLineFile.getValidationResult();
    if (validationResult == null) {
      return null;
    }
    return validationResult;
  }

  /**
   * Returns option name regardless it is long or short.
   *
   * @param o option
   * @return name (if any)
   */
  @Nullable
  @NonNls
  static String getOptionName(@NotNull final CommandLineOption o) {
    final PsiElement longNameToken = o.getLongOptionNameToken();
    if (longNameToken != null) {
      return longNameToken.getText();
    }
    final PsiElement shortOptionNameToken = o.getShortOptionNameToken();
    if (shortOptionNameToken != null) {
      return shortOptionNameToken.getText();
    }
    return null;
  }
}
